/**
  扩展一个http模块
**/      
 
layui.define(['laytpl', 'layer', 'table','upload','tableSelect','layedit'],function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
    var  $ = layui.jquery,
    uploadInst = layui.upload,
    table = layui.table,
    layedit =layui.layedit,
    tableSelect=layui.tableSelect,
    obj = {
     api:"http://admin.zhangxiao521.com/mobile/", 
     token:"admin",
     authToken: "",
     ts: new Date().getTime(),
     loginpage:"http://"+window.location.hostname+"/login.html",
     verifyToken:function(){ //校验登录
        var localtoken = layui.data(obj.token);
        if(!JSON.stringify(localtoken).length){
          window.location.href = obj.loginpage;
        }else{
          if(localtoken.token){
            obj.authToken = localtoken.token;
          }else{
            window.location.href = obj.loginpage;
          }
        }
     },
     exitToken:function(){ //退出登录
        layui.data(obj.token, {key:"admininfo",remove: true});
        layui.data(obj.token, {key:"token",remove: true});
        layui.data(obj.token, {key:"project",remove: true});
        //跳转到登入页 
        window.location.href = obj.loginpage;
     },
     httpajax:function(url,data,type="GET",callback){ //退出登录
        console.error("authToken----",layui.data(obj.token)['token']);
        console.error("执行请求----",url,data,type);
        $.ajax({
          type:type,
          url: obj.api + url,
          data:data,
          dataType: 'json',
          beforeSend: function(request) {
              request.setRequestHeader("Authorization", layui.data(obj.token)['token']);
              request.setRequestHeader("Prokey", layui.data(obj.token)['project'].key);
          },
          success: function(result) {
            console.error("回调种---请求-完成---",result);
            callback(result);
            return result;
          },
          error: function(e, code){
            layer.alert('请求异常，请重试<br><cite>错误信息：</cite>'+ code);
            return false;
          }
      });
     },
     getQueryString:function(name){
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        };
        return null;
     },
     open:function(title,url){
          var index = layer.open({
              title: title,
              type: 2,
              shade: 0.2,
              maxmin:true,
              shadeClose: true,
              area: ['100%', '100%'],
              content:url,
          });
          $(window).on("resize", function () {
              layer.full(index);
          });
     },
     render:function(url,cols,resulti){
          table.render({
              elem: '#currentTableId',
              url:  obj.api + url,
              toolbar: '#toolbarDemo',
              defaultToolbar: ['filter', 'exports', 'print', {
                  title: '提示',
                  layEvent: 'LAYTABLE_TIPS',
                  icon: 'layui-icon-tips'
              }],
              cols: cols,
              where: resulti,
              headers: { //通过 request 头传递
                  Authorization:  layui.data(obj.token)['token'],
                  Prokey:layui.data(obj.token)['project'].key
              },
              done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);
                //得到当前页码
                console.log(curr); 
                //得到数据总量
                console.log(count);
                if(res.code == 403){
                  layer.alert('请求异常，请重试<br><cite>错误信息：</cite>'+ res.msg);
                  window.location.href = obj.loginpage;
                }
              },
              limits: [10, 15, 20, 25, 50, 100],
              limit: 15,
              page: true,
              skin: 'line'
        });
     },
     tableSele:function(elem,searchPlaceholder,url,cols){
        tableSelect.render({
          elem: elem,
          searchKey: 'my',
          checkedKey: 'id',
          searchPlaceholder: searchPlaceholder,
          table: {
              url:  obj.api + url,
              cols:cols,
              headers: { //通过 request 头传递
                Authorization:  layui.data(obj.token)['token'],
                Prokey:layui.data(obj.token)['project'].key
              }
          },
          done: function (elem, data) {
              var NEWJSON = []
              layui.each(data.data, function (index, item) {
                  // NEWJSON.push(item.username)
                  NEWJSON.push(item.id)
              })
              elem.val(NEWJSON.join(","))
          }
        })
     },
     uplode:function(cateBtn,callback){
        uploadInst.render({
            elem: '#'+cateBtn,
            url:obj.api+'/Register/upload',
            done: function(result){
                callback(result);
                return result;
            },
            error: function(e, code){
                layer.alert('请求异常，请重试<br><cite>错误信息：</cite>'+ code);
                return false;
            }
        });
     }
      //根据自己项目的端口而定
    };
    
    //输出test接口
    exports('http', obj);
  });
 